﻿/********************************************************
 *  ██████╗  ██████╗████████╗██╗
 * ██╔════╝ ██╔════╝╚══██╔══╝██║
 * ██║  ███╗██║        ██║   ██║
 * ██║   ██║██║        ██║   ██║
 * ╚██████╔╝╚██████╗   ██║   ███████╗
 *  ╚═════╝  ╚═════╝   ╚═╝   ╚══════╝
 * Geophysical Computational Tools & Library (GCTL)
 *
 * Copyright (c) 2023  Yi Zhang (yizhang-geo@zju.edu.cn)
 *
 * GCTL is distributed under a dual licensing scheme. You can redistribute 
 * it and/or modify it under the terms of the GNU Lesser General Public 
 * License as published by the Free Software Foundation, either version 2 
 * of the License, or (at your option) any later version. You should have 
 * received a copy of the GNU Lesser General Public License along with this 
 * program. If not, see <http://www.gnu.org/licenses/>.
 * 
 * If the terms and conditions of the LGPL v.2. would prevent you from using 
 * the GCTL, please consider the option to obtain a commercial license for a 
 * fee. These licenses are offered by the GCTL's original author. As a rule, 
 * licenses are provided "as-is", unlimited in time for a one time fee. Please 
 * send corresponding requests to: yizhang-geo@zju.edu.cn. Please do not forget 
 * to include some description of your company and the realm of its activities. 
 * Also add information on how to contact you by electronic and paper mail.
 ******************************************************/

#ifndef _GCTL_BLOCK_H
#define _GCTL_BLOCK_H

#include "vertex.h"
#include "entity.h"

namespace gctl
{
	// Declaration of the basic block type
	template <typename A> struct type_block;

	typedef type_block<void> block; // block type of attribute type of void

	/**
	 * 
	 * @brief      structure of a regular block under the Cartesian coordinates.
	 * 
	 * @tparam     A     Attribute type
	 * 
	 * Coordinate system used in the algorithm.
	 * 
	 * z   y
	 * |  /      7      6
	 * | /      /------ur
	 * |/      /|     /|
	 * |      / |    / |
	 * |     /  3---/--/ 2
	 * |    4-----5|  /
	 * |    |      | /
	 * |   dl------|/
	 * |    0      1
	 * O-----------------> x
	 * 
	 */
	template <typename A>
	struct type_block : public entity<vertex3dc, 8, A>
	{
		/**
		 * the down left and up right corners of the block. These two points efficiently define 
		 * dimensions of the block.
		 */
		vertex3dc *dl, *ur;
		/**
		 * neighbors of the block stored in the order x neighbor on left, x neighbor on right, 
		 * y neighbor in front, y neighbor in back, z neighbor in bottom and z neighbor in top.
		 */
		type_block<A> *neigh[6];
		/**
		 * constructor
		 */
		type_block();
		/**
		 * @brief      Constructs an object from a vertex array
		 *
		 * @param[in]  vert_arr  The vertex array
		 * @param[in]  n0_id     The vertex 0 index
		 * @param[in]  n1_id     The vertex 1 index
		 * @param[in]  n2_id     The vertex 2 index
		 * @param[in]  n3_id     The vertex 3 index
		 * @param[in]  n4_id     The vertex 4 index
		 * @param[in]  n5_id     The vertex 5 index
		 * @param[in]  n6_id     The vertex 6 index
		 * @param[in]  n7_id     The vertex 7 index
		 * @param[in]  index     The element index
		 */
		type_block(const array<vertex3dc> &vert_arr, int n0_id, int n1_id, int n2_id, int n3_id, 
			int n4_id, int n5_id, int n6_id, int n7_id, int index = 0);
		/**
		 * @brief      Construct a new block object from parameters
		 * 
		 * @warning    This function will locate memories to store vertice
		 *
		 * @param[in]  xmin   Minimal coordinates of x
		 * @param[in]  xmax   Maximal coordinates of x
		 * @param[in]  ymin   Minimal coordinates of y
		 * @param[in]  ymax   Maximal coordinates of y
		 * @param[in]  zmin   Minimal coordinates of z
		 * @param[in]  zmax   Maximal coordinates of z
		 * @param[in]  index  The element index
		 */
		type_block(double xmin, double xmax, double ymin, double ymax, double zmin, double zmax, int index = 0);
		/**
		 * @brief      de-constructor
		 */
		virtual ~type_block(){}
		/**
		 * @brief      Constructs an object from a vertex array
		 *
		 * @param[in]  vert_arr  The vertex array
		 * @param[in]  n0_id     The vertex 0 index
		 * @param[in]  n1_id     The vertex 1 index
		 * @param[in]  n2_id     The vertex 2 index
		 * @param[in]  n3_id     The vertex 3 index
		 * @param[in]  n4_id     The vertex 4 index
		 * @param[in]  n5_id     The vertex 5 index
		 * @param[in]  n6_id     The vertex 6 index
		 * @param[in]  n7_id     The vertex 7 index
		 * @param[in]  index     The element index
		 */
		void set(const array<vertex3dc> &vert_arr, int n0_id, int n1_id, int n2_id, int n3_id, 
			int n4_id, int n5_id, int n6_id, int n7_id, int index = 0);
		/**
		 * @brief      Construct a new block object from parameters
		 * 
		 * @warning    This function will locate memories to store vertice
		 *
		 * @param[in]  xmin   Minimal coordinates of x
		 * @param[in]  xmax   Maximal coordinates of x
		 * @param[in]  ymin   Minimal coordinates of y
		 * @param[in]  ymax   Maximal coordinates of y
		 * @param[in]  zmin   Minimal coordinates of z
		 * @param[in]  zmax   Maximal coordinates of z
		 * @param[in]  index  The element index
		 */
		void set(double xmin, double xmax, double ymin, double ymax, double zmin, double zmax, int index = 0);
		/**
         * @brief      Reset the structure to initial state
         */
        void reset();
	};

	template <typename A>
	type_block<A>::type_block() : entity<vertex3dc, 8, A>::entity()
	{
		dl = ur = nullptr;
		for (int i = 0; i < 6; ++i)
		{
			neigh[i] = nullptr;
		}
	}

	template <typename A>
	type_block<A>::type_block(const array<vertex3dc> &vert_arr, int n0_id, int n1_id, int n2_id, int n3_id, 
		int n4_id, int n5_id, int n6_id, int n7_id, int index) : type_block()
	{
		set(vert_arr, n0_id, n1_id, n2_id, n3_id, n4_id, n5_id, n6_id, n7_id, index);
	}

	template <typename A>
	type_block<A>::type_block(double xmin, double xmax, double ymin, double ymax, double zmin, 
		double zmax, int index) : type_block()
	{
		set(xmin, xmax, ymin, ymax, zmin, zmax, index);
	}

	template <typename A>
	void type_block<A>::set(const array<vertex3dc> &vert_arr, int n0_id, int n1_id, int n2_id, int n3_id, 
		int n4_id, int n5_id, int n6_id, int n7_id, int index)
	{
		if (index < 0)
	    {
	        throw out_of_range("Invalid index number, From type_block::set(...)");
	    }

	    if (vert_arr.size() < 8)
	    {
	        throw length_error("Invalid array size, From type_block::set(...)");
	    }

	    if (vert_arr[n0_id].x >= vert_arr[n6_id].x || vert_arr[n0_id].y >= vert_arr[n6_id].y 
	    	|| vert_arr[n0_id].z >= vert_arr[n6_id].z)
	    {
	        throw invalid_argument("Invalid vertex's coordinates. From type_block::set(...)");
	    }

		this->id = index;
		this->vert[0] = vert_arr.get(n0_id);
		this->vert[1] = vert_arr.get(n1_id);
		this->vert[2] = vert_arr.get(n2_id);
		this->vert[3] = vert_arr.get(n3_id);
		this->vert[4] = vert_arr.get(n4_id);
		this->vert[5] = vert_arr.get(n5_id);
		this->vert[6] = vert_arr.get(n6_id);
		this->vert[7] = vert_arr.get(n7_id);
		dl = this->vert[0];
		ur = this->vert[6];
		return;
	}

	template <typename A>
	void type_block<A>::set(double xmin, double xmax, double ymin, double ymax, double zmin, 
		double zmax, int index)
	{
		if (index < 0)
	    {
	        throw out_of_range("Invalid index number, From type_block::set(...)");
	    }

		if (xmin >= xmax || ymin >= ymax || zmin >= zmax)
	    {
			throw invalid_argument("Invalid vertex's coordinates. From type_block::set(...)");
	    }

	    for (int i = 0; i < 8; ++i)
	    {
	    	this->vert[i] = new vertex3dc;
	    }
	    this->self_host = true;

		this->id = index;
		this->vert[0]->set(point3dc(xmin, ymin, zmin), 0);
		this->vert[1]->set(point3dc(xmax, ymin, zmin), 1);
		this->vert[2]->set(point3dc(xmax, ymax, zmin), 2);
		this->vert[3]->set(point3dc(xmin, ymax, zmin), 3);
		this->vert[4]->set(point3dc(xmin, ymin, zmax), 4);
		this->vert[5]->set(point3dc(xmax, ymin, zmax), 5);
		this->vert[6]->set(point3dc(xmax, ymax, zmax), 6);
		this->vert[7]->set(point3dc(xmin, ymax, zmax), 7);
		dl = this->vert[0];
		ur = this->vert[6];
		return;
	}

	template <typename A>
	void type_block<A>::reset()
	{
	    entity<vertex3dc, 8, A>::reset();
	    dl = ur = nullptr;
	    for (int i = 0; i < 6; ++i)
		{
			neigh[i] = nullptr;
		}
	    return;
	}

	template <typename A>
	void copy_type_block(type_block<A> *tar, const type_block<A> *src)
	{
		copy_entity(tar, src);

		tar->dl = src->dl;
		tar->ur = src->ur;

		for (size_t i = 0; i < 6; i++)
		{
			tar->neigh[i] = src->neigh[i];
		}
		return;
	}

	template <typename A, typename B>
	void copy_type_block(type_block<A> *tar, const type_block<B> *src)
	{
		copy_entity(tar, src);

		tar->dl = src->dl;
		tar->ur = src->ur;
		return;
	}
}

#endif // _GCTL_BLOCK_H